﻿#region File and License Information
/*
<File>
	<License Type="BSD">
		Copyright © 2009 - 2012, Outcoder. All rights reserved.
	
		This file is part of Calcium (http://calciumsdk.net).

		Redistribution and use in source and binary forms, with or without
		modification, are permitted provided that the following conditions are met:
			* Redistributions of source code must retain the above copyright
			  notice, this list of conditions and the following disclaimer.
			* Redistributions in binary form must reproduce the above copyright
			  notice, this list of conditions and the following disclaimer in the
			  documentation and/or other materials provided with the distribution.
			* Neither the name of the <organization> nor the
			  names of its contributors may be used to endorse or promote products
			  derived from this software without specific prior written permission.

		THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
		ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
		WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
		DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
		DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
		(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
		LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
		ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
		(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
		SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
	</License>
	<Owner Name="Daniel Vaughan" Email="danielvaughan@outcoder.com" />
	<CreationDate>2009-10-30 13:06:34Z</CreationDate>
</File>
*/
#endregion

using System;

#if NETFX_CORE
using Dispatcher=Windows.UI.Core.CoreDispatcher;
using DispatcherPriority=Windows.UI.Core.CoreDispatcherPriority;
#else
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Threading;
#endif

namespace Outcoder
{
	/* TODO: [DV] Comment. */
	public static class DispatcherExtensions
	{

#if !SILVERLIGHT && !NETFX_CORE
		/// <summary>
		/// A simple threading extension method, to invoke a delegate
		/// on the correct thread if it is not currently on the correct thread
		/// which can be used with DispatcherObject types.
		/// </summary>
		/// <param name="dispatcher">The Dispatcher object on which to 
		/// perform the Invoke</param>
		/// <param name="action">The delegate to run</param>
		/// <param name="priority">The DispatcherPriority for the invoke.</param>
		public static void InvokeIfRequired(this Dispatcher dispatcher,
			Action action, DispatcherPriority priority)
		{
			if (!dispatcher.CheckAccess())
			{
				dispatcher.Invoke(priority, action);
			}
			else
			{
				action();
			}
		}
#endif

		/// <summary>
		/// A simple threading extension method, to invoke a delegate
		/// on the correct thread if it is not currently on the correct thread
		/// which can be used with DispatcherObject types.
		/// </summary>
		/// <param name="dispatcher">The Dispatcher object on which to 
		/// perform the Invoke</param>
		/// <param name="action">The delegate to run</param>
		public static void InvokeIfRequired(this Dispatcher dispatcher, Action action)
		{
#if NETFX_CORE
			if (!dispatcher.HasThreadAccess)
			{
#else
			if (!dispatcher.CheckAccess())
			{
#endif
			
#if SILVERLIGHT
				dispatcher.BeginInvoke(action);
#elif NETFX_CORE
#pragma warning disable 4014
				dispatcher.RunAsync(DispatcherPriority.Normal, () => action());
#pragma warning restore 4014
#else
				dispatcher.Invoke(DispatcherPriority.Normal, action);
#endif
			}
			else
			{
				action();
			}
		}

		/// <summary>
		/// A simple threading extension method, to invoke a delegate
		/// on the correct thread if it is not currently on the correct thread
		/// which can be used with DispatcherObject types.
		/// </summary>
		/// <param name="dispatcher">The Dispatcher object on which to 
		/// perform the Invoke</param>
		/// <param name="action">The delegate to run</param>
//		public static void InvokeInBackgroundIfRequired(this Dispatcher dispatcher, Action action)
//		{
//			if (!dispatcher.CheckAccess())
//			{
//#if SILVERLIGHT
//				dispatcher.BeginInvoke(action);
//#else
//				dispatcher.Invoke(DispatcherPriority.Background, action);
//#endif
//			}
//			else
//			{
//				action();
//			}
//		}

		/// <summary>
		/// A simple threading extension method, to invoke a delegate
		/// on the correct thread asynchronously if it is not currently on the correct thread
		/// which can be used with DispatcherObject types.
		/// </summary>
		/// <param name="dispatcher">The Dispatcher object on which to 
		/// perform the Invoke</param>
		/// <param name="action">The delegate to run</param>
		public static void InvokeWithoutBlocking(this Dispatcher dispatcher, Action action)
		{
#if SILVERLIGHT
			dispatcher.BeginInvoke(action);
#elif NETFX_CORE
#pragma warning disable 4014
			dispatcher.RunAsync(DispatcherPriority.Low, () => action());
#pragma warning restore 4014
#else
			dispatcher.BeginInvoke(DispatcherPriority.Background, action);
#endif
		}

	}
}
